home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / stereo / opengl / simple_stereo_re.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.8 KB  |  339 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18.  
  19. #include <Xm/Xm.h>
  20. #include <X11/Intrinsic.h>
  21. #include <X11/extensions/SGIStereo.h>
  22.  
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glx.h>
  26. #include <GL/GLwMDrawA.h>
  27.  
  28. #define Color_Buffer_Size        1
  29. #define Depth_Buffer_Size        1
  30.  
  31. int                    sphere_slices = 16,
  32.                     sphere_stacks = 16,
  33.                     xRot = 0, yRot = 0,
  34.                     prevy = 0, prevx = 0,
  35.                     nan_stereo_flag = 0;
  36.  
  37.  
  38. void                    initCB(Widget, XtPointer, XtPointer),
  39.                     exposeCB(Widget, XtPointer, XtPointer),
  40.                     resizeCB(Widget, XtPointer, XtPointer);
  41.  
  42. void                    SceneDraw(Display *, Window),
  43.                     nanWireSpheref(float, float, float, float),
  44.                     nanSolidSpheref(float, float, float, float);
  45.  
  46. void                     start_rtn(Widget, XEvent*, String*, Cardinal*),
  47.                     rtn(Widget, XEvent*, String*, Cardinal*),
  48.                     end_rtn(Widget, XEvent*, String*, Cardinal*),
  49.                     stereoEnable(Widget, XEvent*, String*, Cardinal*);
  50.  
  51.  
  52.  
  53. main(int argc, char **argv)
  54. {
  55.   XtAppContext        application_context;
  56.   Widget        toplevel, glw;
  57.   Arg            args[10];
  58.   int             n = 0;
  59.   GLXContext        glw_context;
  60.   XtTranslations    trans;
  61.   String         fallback[] = {
  62.                           "*frame*shadowType: SHADOW_IN",
  63.                       "*boiler*width: 400",
  64.                       "*boiler*height: 400",
  65.                       NULL
  66.                       };
  67.   
  68.   char             *molTranslations =
  69.                           "#override\n\
  70.                                          <Btn1Down>:start_rtn() \n\
  71.                                          <Btn1Motion>:rtn() \n\
  72.                                          <Btn1Up>:end_rtn() \n\
  73.                          <Btn2Down>:stereoEnable()\n";
  74.  
  75.   XtActionsRec        actionsTable[] = {
  76.                           {"start_rtn", start_rtn},
  77.                       {"rtn", rtn},
  78.                       {"end_rtn", end_rtn},
  79.                       {"stereoEnable", stereoEnable}
  80.                      };
  81.   
  82.   toplevel = XtAppInitialize(&application_context, "BoilerPlate",
  83.                  (XrmOptionDescList) NULL, 0,
  84.                  &argc, (String *)argv,
  85.                  fallback, (ArgList)NULL, 0);
  86.  
  87.   trans = XtParseTranslationTable(molTranslations);
  88.   XtAppAddActions(application_context, actionsTable, XtNumber(actionsTable));
  89.  
  90.   n=0;
  91.   XtSetArg(args[n], GLwNrgba, TRUE); n++;
  92.   XtSetArg(args[n], GLwNdoublebuffer, TRUE); n++;
  93.   XtSetArg(args[n], GLwNredSize, Color_Buffer_Size); n++;
  94.   XtSetArg(args[n], GLwNgreenSize, Color_Buffer_Size); n++;
  95.   XtSetArg(args[n], GLwNblueSize, Color_Buffer_Size); n++;
  96.   XtSetArg(args[n], GLwNdepthSize, Depth_Buffer_Size); n++;
  97.   XtSetArg(args[n], GLwNstereo, True); n++;
  98.  
  99.   glw = XtCreateWidget("boiler", glwMDrawingAreaWidgetClass, 
  100.                toplevel, args, n);
  101.   
  102.   XtAddCallback(glw, GLwNginitCallback, initCB, &glw_context);
  103.   XtAddCallback(glw, GLwNexposeCallback, exposeCB, &glw_context);
  104.   XtAddCallback(glw, GLwNresizeCallback, resizeCB, &glw_context);
  105.  
  106.   XtOverrideTranslations(glw, trans);
  107.   XtManageChild(glw);
  108.   
  109.   XtRealizeWidget(toplevel);
  110.   XtAppMainLoop(application_context);
  111. }
  112.  
  113. void 
  114. initCB(Widget w, XtPointer client_data, XtPointer call_data)
  115. {
  116.   Display    *display = XtDisplay(w);
  117.   int        screen = DefaultScreen(display);
  118.   Window    window = XtWindow(w);
  119.   XVisualInfo    *vi;
  120.   GLXContext    *glw_context = (GLXContext *) client_data;
  121.   Arg        args[10];
  122.   float        mat_specular[] = { .72, .8, .93, 1.0 };
  123.  
  124.   XtSetArg(args[0], GLwNvisualInfo, &vi);
  125.   XtGetValues(w, args, 1);
  126.   
  127.   *glw_context = glXCreateContext(display, vi, None, GL_TRUE);
  128.   glXMakeCurrent(display, window, *glw_context);
  129.  
  130.   glMatrixMode(GL_PROJECTION);
  131.   glLoadIdentity();
  132.   gluPerspective(60.0,  1.0,  .25,  15.0);
  133.   glMatrixMode(GL_MODELVIEW);
  134.   glLoadIdentity();
  135.   glTranslatef(0.0, 0.0, -6.0);
  136.  
  137.   glDepthFunc(GL_LEQUAL);
  138.   glEnable(GL_DEPTH_TEST);
  139.   
  140.   glClearColor(0.0, 0.0, 0.0, 1.0);
  141.   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  142.   glXSwapBuffers(display, window);
  143.   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  144.  
  145.   glEnable(GL_LINE_SMOOTH);
  146.   glEnable(GL_BLEND);
  147.   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  148.   glLineWidth(1.5);
  149.  
  150.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  151.   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 128.0);
  152.   
  153.   glEnable(GL_LIGHT0);
  154.   glEnable(GL_LIGHTING);
  155.   glEnable(GL_NORMALIZE);
  156.  
  157.   SceneDraw(display, window);
  158. }
  159.  
  160. void
  161. exposeCB(Widget w, XtPointer client_data, XtPointer call_data)
  162. {
  163.   Display       *display = XtDisplay(w);
  164.   Window    window = XtWindow(w);
  165.   GLXContext    *glw_context = (GLXContext *) client_data;
  166.  
  167.   glXMakeCurrent(display, window, *glw_context);
  168.   SceneDraw(display, window);
  169. }
  170.  
  171. void
  172. resizeCB(Widget w, XtPointer client_data, XtPointer call_data)
  173. {
  174.   Display       *display = XtDisplay(w);
  175.   Window        window = XtWindow(w);
  176.   GLXContext    *glw_context = (GLXContext *) client_data;
  177.   GLwDrawingAreaCallbackStruct *Call_Data = 
  178.                        (GLwDrawingAreaCallbackStruct *) call_data;
  179.   
  180.   glXMakeCurrent(display, window, *glw_context);
  181.   glViewport(0, 0,
  182.          (GLuint) Call_Data->width-1,
  183.          (GLuint) Call_Data->height-1);
  184.   
  185.   SceneDraw(display, window);
  186. }
  187.  
  188.  
  189. void SceneDraw(Display *d, Window w)
  190. {
  191.   
  192.   glPushMatrix();
  193.   glRotatef(2.0, 0.0, 1.0, 0.0);
  194.   glDrawBuffer(GL_BACK_LEFT);
  195.   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  196.  
  197.   glColor3f(1.0, 1.0, 0.0);
  198.   nanWireSpheref(0.0, 0.0, -1.0, 1.0);
  199.  
  200.   glColor3f(.5, .5, 5.);
  201.   nanSolidSpheref(0.0, 0.0, 1.0, 1.0);
  202.  
  203.   glColor3f(1.0, 0.0, 0.0);
  204.   nanSolidSpheref(-1.0, 0.0, 0.0, 1.0);
  205.  
  206.   glColor3f(0.0, 1.0, 0.0);
  207.   nanSolidSpheref(1.0, 0.0, 0.0, 1.0);
  208.  
  209.   glColor3f(0.0, 0.0, 1.0);
  210.   nanWireSpheref(0.0, 1.0, 0.0, 1.0);
  211.  
  212.   glColor3f(1.0, 0.0, 1.0);
  213.   nanWireSpheref(0.0, -1.0, 0.0, 1.0);
  214.   glPopMatrix();
  215.  
  216.   if(nan_stereo_flag)
  217.     {
  218.       glPushMatrix();
  219.       glRotatef(-2.0, 0.0, 1.0, 0.0);
  220.       glDrawBuffer(GL_BACK_RIGHT);
  221.       glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  222.  
  223.       glColor3f(1.0, 1.0, 0.0);
  224.       nanWireSpheref(0.0, 0.0, -1.0, 1.0);
  225.  
  226.       glColor3f(.5, .5, 5.);
  227.       nanSolidSpheref(0.0, 0.0, 1.0, 1.0);
  228.  
  229.       glColor3f(1.0, 0.0, 0.0);
  230.       nanSolidSpheref(-1.0, 0.0, 0.0, 1.0);
  231.  
  232.       glColor3f(0.0, 1.0, 0.0);
  233.       nanSolidSpheref(1.0, 0.0, 0.0, 1.0);
  234.  
  235.       glColor3f(0.0, 0.0, 1.0);
  236.       nanWireSpheref(0.0, 1.0, 0.0, 1.0);
  237.  
  238.       glColor3f(1.0, 0.0, 1.0);
  239.       nanWireSpheref(0.0, -1.0, 0.0, 1.0);
  240.       glPopMatrix();
  241.     }
  242.     
  243.   glFlush();
  244.   glXSwapBuffers(d, w);
  245. }
  246.  
  247. void nanSolidSpheref(float x, float y, float z, float radius)
  248. {
  249.   static GLUquadricObj  *quadObj;
  250.   static int            entry = 0;
  251.  
  252.   glEnable(GL_COLOR_MATERIAL);
  253.   glPushMatrix();
  254.   glTranslatef(x, y, z);
  255.   if (!entry)
  256.     {
  257.       quadObj = gluNewQuadric ();
  258.       gluQuadricDrawStyle (quadObj, GLU_FILL);
  259.       gluQuadricOrientation(quadObj, GLU_OUTSIDE);
  260.       gluQuadricNormals (quadObj, GLU_SMOOTH);
  261.     }
  262.   gluSphere (quadObj, radius, sphere_slices, sphere_stacks);
  263.   glPopMatrix();
  264.   glDisable(GL_COLOR_MATERIAL);
  265. }
  266.  
  267. void nanWireSpheref(float x, float y, float z, float radius)
  268. {
  269.   static GLUquadricObj  *quadObj;
  270.   static int            entry = 0;
  271.  
  272.   glEnable(GL_COLOR_MATERIAL);
  273.   glPushMatrix();
  274.   glTranslatef(x, y, z);
  275.   if (!entry)
  276.     {
  277.       quadObj = gluNewQuadric ();
  278.       gluQuadricDrawStyle (quadObj, GLU_LINE);
  279.       gluQuadricOrientation(quadObj, GLU_OUTSIDE);
  280.       gluQuadricNormals (quadObj, GLU_SMOOTH);
  281.     }
  282.   gluSphere (quadObj, radius, sphere_slices, sphere_stacks);
  283.   glPopMatrix();
  284.   glDisable(GL_COLOR_MATERIAL);
  285. }
  286.  
  287. void start_rtn(Widget w, XEvent *event, String *s, Cardinal *c)
  288. {
  289.   prevx = event->xbutton.x;
  290.   prevy = event->xbutton.y;
  291. }
  292.  
  293. void rtn(Widget w, XEvent *event, String *s, Cardinal *c)
  294. {
  295.   int x = event->xbutton.x;
  296.   int y = event->xbutton.y;
  297.  
  298.   yRot +=  5 * (x - prevx);
  299.   xRot +=  5 * (y - prevy);
  300.   prevx = x;
  301.   prevy = y;
  302.   glPushMatrix();
  303.   glRotatef(xRot*0.1, 1.0, 0.0, 0.0);
  304.   glRotatef(yRot*0.1, 0.0, 1.0, 0.0);
  305.   SceneDraw(XtDisplay(w), XtWindow(w));
  306.   glPopMatrix();
  307. }
  308.  
  309. void
  310. end_rtn(Widget w, XEvent *event, String *s, Cardinal *c)
  311. {
  312.   glPushMatrix();
  313.   glRotatef(xRot*0.1, 1.0, 0.0, 0.0);
  314.   glRotatef(yRot*0.1, 0.0, 1.0, 0.0);
  315.   SceneDraw(XtDisplay(w), XtWindow(w));
  316.   glPopMatrix();
  317. }    
  318.  
  319. void
  320. stereoEnable(Widget w, XEvent *event, String *s, Cardinal *c)
  321. {
  322.   static    mode = 0;
  323.  
  324.   nan_stereo_flag = !nan_stereo_flag;
  325.   if(nan_stereo_flag)
  326.     {
  327.       system("/usr/gfx/setmon -n 1025x768_96s");
  328.     }
  329.   else
  330.     {
  331.       system("/usr/gfx/setmon -n 72HZ");
  332.     }
  333.   glPushMatrix();
  334.   glRotatef(xRot*0.1, 1.0, 0.0, 0.0);
  335.   glRotatef(yRot*0.1, 0.0, 1.0, 0.0);
  336.   SceneDraw(XtDisplay(w), XtWindow(w));
  337.   glPopMatrix();
  338. }
  339.